'''从serpapi中抓取数据，然后存入到数据库中'''
from flask_restful import Resource, reqparse

from Config import Configs
from services.scrap_serp import ScrapDataFromGoogle
from services.store_data import StoreData
from controllers import api
from extensions.ext_database import db
from models.serp import SerpResult
from dateutil import parser
from datetime import datetime

# 调用接口后触发开始从谷歌获取数据然后存储到数据库中
class GetDataFromGoogle(Resource):

    def get(self):
        config = Configs()
        search_term = {
            "q": config.SEARCH_TEXT,
            "location": config.LOCATION,
            "api_key": config.SERP_API_KEY,
            "num": config.SERP_NUM
        }

        result = ScrapDataFromGoogle.start_scrap(search_term)
        StoreData.store_google(result)

        return {'result': 'success'}


class NewsLists(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('title', type=str, required=False, nullable=True, location='args')
        args = parser.parse_args()
        title = args['title']
        query = db.session.query(SerpResult)
        if title:
            query = query.filter(SerpResult.title == title)
        news = query.all()

        news = [
            {
                'id': str(new.id),
                'title': new.title,
                'link': new.link,
                'source': new.source,
                'date': convert_date(new.date),
                'snippet': new.snippet,
                'create_at': new.created_at.isoformat(),
            }
            for new in news
        ]
        news.sort(key=lambda x: parse_date(x['date']), reverse=True)

        return {'news_list': news}


#统一将数据库中的三种不同格式的日期转化为统一格式赋值
def convert_date(date_str):
    if date_str == 'N/A':
        return None
    try:
        parsed_date = parser.parse(date_str, fuzzy=True)
        return parsed_date.isoformat()
    except ValueError:
        return None

#排序的时候转化为datetime对象
def parse_date(date_str):
    if date_str is None:
        return datetime.min
    try:
        return parser.isoparse(date_str)
    except ValueError:
        return datetime.min

# 加入api接口中
api.add_resource(GetDataFromGoogle, '/GetDataFromGoogle')
api.add_resource(NewsLists, '/NewsLists')
